/**
 * @param {number[][]} intervals
 * @return {number[][]}
 */
 var merge = function(intervals) {
   intervals.sort((a,b)=>a[0]-b[0])
   
  let res = [intervals[0]]
  let count =0
  for(let i=1;i<intervals.length;i++){
    if(intervals[i][0]==res[count][1]){
      res[count] = [res[count][0],intervals[i][1]]
    }else if(intervals[i][0]>res[count][1]){
      res.push(intervals[i])
      count++
    }else if(intervals[i][0]>=res[count][0]&& intervals[i][0]<=res[count][1]){
      if(intervals[i][1]>res[count][1]){
        res[count]=[res[count][0],intervals[i][1]]
      }
    }
  } 
  return res
};

merge([[1,4],[4,5]])
